const query = require("./index")

module.exports = {
  login(username, password) {
    // 登录
    const sql = `SELECT * FROM users WHERE user_name=? AND password=?`

    return query(sql, [username, password])
  },
  register(username, password, branchId) {
    // 注册
    const sql = `INSERT INTO users VALUES(NULL,?,?,?,DEFAULT)`

    return query(sql, [username, password, branchId])
  },
  getRole(userId) {
    // 获取用户角色
    const sql = `SELECT r.role_id roleId,r.role_name roleName FROM user_role ur
    INNER JOIN role r
    ON ur.role_id=r.role_id
    WHERE ur.user_id=? AND r.is_enabled`

    return query(sql, [userId])
  },
  getBranch(branchId) {
    // 获取用户部门
    const sql = `SELECT branch_name branchName FROM branch WHERE branch_id=?`

    return query(sql, [branchId])
  },
  getPermission(roleId) {
    // 获取用户菜单权限表
    const sql = `SELECT m.menu_id menuId,m.pid pid,m.menu_name menuName,m.icon icon,m.page_url pageUrl,m.component component,m.control_url controlUrl,m.is_show isShow,m.is_exact isExact FROM menu m
    INNER JOIN role_menu rm
    ON rm.menu_id=m.menu_id
    WHERE rm.role_id=? AND m.is_enabled`

    return query(sql, [roleId])
  },
  total() {
    const sql = `SELECT count(*) as totalUser FROM users`

    return query(sql, [null])
  },
}
